﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxUploadControl;

namespace Project_Udrinkwater.PagesAdmin
{
    public partial class Add_And_Edit_Goods : System.Web.UI.Page
    {
        ImageTosqlDataContext db = new ImageTosqlDataContext();
        // DataClassesProductDataContext dbp = new DataClassesProductDataContext();
        string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["GoodID"]==null)
                {
                    int i = GetID();
                    string idGen = string.Format("PD-{0}", i);
                    tb_GoodsIDInsert.Text = idGen;

                    tb_GoodsName.Text = "";
                    tb_Price.Text = "";
                    tb_PriceRetial.Text = "";
                 //   tb_Loan.Text = "";
                   // tb_GoodsIDInsert.Text = "";
                    Pn_Insert.Visible = true;
                }
                else
                {
                    EditGoodsdata(Session["GoodID"].ToString());
                }
               
            }
            
        }
        private int GetID()
        {
            int id;
            string sqltext = @"SELECT ISNULL (MAX(id),0)
                                from [TB_Product]";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            try
            {
                sqlcomm.Connection.Open();
                id = (int)sqlcomm.ExecuteScalar() + 1;
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
                throw;
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();
            return id;
        }
       
        public void EditGoodsdata(string GoodID)
        {
            //ดึงข้อมูลมาแก้ไข
            Pn_Update.Visible = true;
            Pn_Insert.Visible = false;
            Image1.Visible = true;
            var q = (from a in db.TB_Products
                     where a.Product_ID == GoodID
                     select a).First();
            tb_GoodsIDUpdate.Text = q.Product_ID;
            tb_GoodsNameUpdate.Text = q.Product_Name;
            //tb_PriceUpdateDetial.Text = q.Price.ToString();
            //tb_LoanUpdate.Text = q.Price_Loan.ToString();
            Image1.ImageUrl = q.Image;
            cb_UnitUpdate.SelectedIndex = Convert.ToInt32(q.Product_Unit-1);
            Session["UrlImage"] = q.Image;

            var c = (from b in db.TB_Order_Categories
                     where b.Product_ID == GoodID & b.Order_Category_Name == "ขายส่ง"
                     select b).First();
            tb_PriceUpdate.Text = c.Price.ToString();

            var D = (from b in db.TB_Order_Categories
                     where b.Product_ID == GoodID & b.Order_Category_Name == "ขายปลีก"
                     select b).First();
            tb_PriceUpdateDetial.Text = D.Price.ToString();

            var E = (from b in db.TB_Order_Categories
                     where b.Product_ID == GoodID & b.Order_Category_Name == "ยืม-คืน"
                     select b).First();
            tb_LoanPriceUpdate.Text = E.Price.ToString();
            if (tb_LoanPriceUpdate.Text =="")
            {
                tb_LoanPriceUpdate.Visible = true;
            }
        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.PostedFile != null)
            {
                string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                FileUpload1.SaveAs(Server.MapPath("~/Images_Goods/" + FileName));

                //เพิ่มข้อมูลตารางสินค้า และรูปภาพ
                //TB_Product Tbimg = new TB_Product();
                //Tbimg.Product_ID = tb_GoodsIDInsert.Text;
                //Tbimg.Product_Name = tb_GoodsName.Text;
                //Tbimg.Price = Convert.ToInt32(tb_PriceRetial.Text);
                //Tbimg.Price_Loan = Convert.ToInt32(tb_Loan.Text);
                //Tbimg.Unit = cb_UnitInsert.SelectedItem.Text;
                //Tbimg.Image_Path = "~/Images_Goods/" + FileName;
                //db.TB_ImagePaths.InsertOnSubmit(Tbimg);
                //db.SubmitChanges();

                //เพิ่มสินค้าใหม่ที่ Table Product
                TB_Product TbPro = new TB_Product();
                TbPro.Product_ID = tb_GoodsIDInsert.Text;
                TbPro.Product_Name = tb_GoodsName.Text;
                TbPro.Product_Category_ID = "01";
                TbPro.Product_Unit = Convert.ToInt32(cb_UnitInsert.Value);
                TbPro.Image = "~/Images_Goods/" + FileName;
                TbPro.In_Stock = 0;
                db.TB_Products.InsertOnSubmit(TbPro);
                db.SubmitChanges();

                //เพิ่มราคาขายสินค้าขายส่ง
                TB_Order_Category tbO = new TB_Order_Category();
                tbO.Order_Category_Name = "ขายส่ง";
                tbO.Product_ID = tb_GoodsIDInsert.Text;
                tbO.Price = Convert.ToInt32(tb_Price.Text);
                db.TB_Order_Categories.InsertOnSubmit(tbO);
                db.SubmitChanges();

                //เพิ่มราคาขายสินค้าขายปลีก
                TB_Order_Category tbO1 = new TB_Order_Category();
                tbO1.Order_Category_Name = "ขายปลีก";
                tbO1.Product_ID = tb_GoodsIDInsert.Text;
                tbO1.Price = Convert.ToInt32(tb_PriceRetial.Text);
                db.TB_Order_Categories.InsertOnSubmit(tbO1);
                db.SubmitChanges();

                if (cb_UnitInsert.Value == "1" || cb_UnitInsert.Value == "2")
                {
                    TB_Order_Category tbO2 = new TB_Order_Category();
                    tbO2.Order_Category_Name = "ยืม-คืน";
                    tbO2.Product_ID = tb_GoodsIDInsert.Text;
                    tbO2.Price = Convert.ToInt32(tb_LoanPrice.Text);
                    db.TB_Order_Categories.InsertOnSubmit(tbO2);
                    db.SubmitChanges();
                }

                tb_GoodsName.Text = "";
                tb_Price.Text = "";
               // tb_Loan.Text = "";
                tb_GoodsIDInsert.Text = "";
                tb_PriceRetial.Text = "";
                Pn_Insert.Visible = false;

                Response.Redirect("GoodsList.aspx");
               // ASPxGridView1.DataBind();
            }
        }

        protected void bt_Cancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("GoodsList.aspx");
        }

        protected void bt_CancleUpdate_Click(object sender, EventArgs e)
        {
            Response.Redirect("GoodsList.aspx");
        }

        protected void bt_Update_Click(object sender, EventArgs e)
        {
            var q = (from a in db.TB_Products
                     where a.Product_ID == tb_GoodsIDUpdate.Text
                     select a).First();
            //แก้ไขและบันทึกข้อมูลสินค้า และรูปภาพ
            if (FileUpload2.FileName != "")
            {
                string FileName = Path.GetFileName(FileUpload2.PostedFile.FileName);
                FileUpload2.SaveAs(Server.MapPath("~/Images_Goods/" + FileName));
                FileInfo FileIn = new FileInfo(Server.MapPath(Session["UrlImage"].ToString()));
                FileIn.Delete();
                q.Product_ID = tb_GoodsIDUpdate.Text;
                q.Product_Name = tb_GoodsNameUpdate.Text;
                //q.Price = Convert.ToInt32(tb_PriceUpdateDetial.Text);
                //q.Price_Loan = Convert.ToInt32(tb_LoanUpdate.Text);
                q.Image = "~/Images_Goods/" + FileName;
                q.Product_Unit = Convert.ToInt32(cb_UnitUpdate.Value);
                db.SubmitChanges();
            }
            else
            {
                q.Product_ID = tb_GoodsIDUpdate.Text;
                q.Product_Name = tb_GoodsNameUpdate.Text;
                //q.Price = Convert.ToInt32(tb_PriceUpdateDetial.Text);
                //q.Price_Loan = Convert.ToInt32(tb_LoanUpdate.Text);
                q.Product_Unit = Convert.ToInt32(cb_UnitUpdate.Value);
                db.SubmitChanges();
            }

            //บันทึกการแก้ไขข้อมูลสินค้า
            //var p = (from b in db.TB_Products
            //         where b.Product_ID == tb_GoodsIDUpdate.Text
            //         select b).First();
            //p.Product_ID = tb_GoodsIDUpdate.Text;
            //p.Product_Name = tb_GoodsNameUpdate.Text;
            //p.Product_Unit = cb_UnitUpdate.Value.ToString();
            //db.SubmitChanges();

            var o = (from g in db.TB_Order_Categories
                     where g.Product_ID == tb_GoodsIDUpdate.Text & g.Order_Category_Name == "ขายส่ง"
                     select g).First();
            o.Price = Convert.ToInt32(tb_PriceUpdate.Text);
            db.SubmitChanges();

            var s = (from t in db.TB_Order_Categories
                     where t.Product_ID == tb_GoodsIDUpdate.Text & t.Order_Category_Name == "ขายปลีก"
                     select t).First();
            s.Price = Convert.ToInt32(tb_PriceUpdateDetial.Text);
            db.SubmitChanges();

            if (tb_LoanPriceUpdate.Text != "")
            {
                var u = (from t in db.TB_Order_Categories
                         where t.Product_ID == tb_GoodsIDUpdate.Text & t.Order_Category_Name == "ยืม-คืน"
                         select t).First();
                u.Price = Convert.ToInt32(tb_LoanPriceUpdate.Text);
                db.SubmitChanges();
            }

            tb_GoodsIDUpdate.Text = "";
            tb_GoodsNameUpdate.Text = "";
            tb_PriceUpdate.Text = "";
          //  tb_LoanUpdate.Text = "";
            tb_PriceUpdateDetial.Text = "";
            Image1.ImageUrl = "";
            Image1.Visible = false;
            Pn_Update.Visible = false;

            Response.Redirect("GoodsList.aspx");
           // ASPxGridView1.DataBind();
        }

       

    }
}